/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.groupb.circus.UserInterface;


import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Projecte
 */
public class Presentacio {


    // Mètode que crea arxiu HTML, i l'executa amb el navegador predeterminat. Cal passar-li
    // un PathPhotos que es la carpeta on estaran les fotos,
    // un PathHTML5, que serà on crei l'arxiu HTML que ha d'estar al mateix directori que
    // la carpeta de les fotos, i finalment el nom de l'arxiu.

 public void getPresentacio(String PathPhotos, String PathHTML5, String NameHTML5){

        //Primer de tot, busquem el numero d'imatges que hi ha en un directori
int nombreImatges = nombreImatgesDirectori(PathPhotos);



       String nom = "proba.html";
String html5=""+ "<!DOCTYPE HTML>"+"\n"+
            "<html>"+"\n"+
            "<head>"+"\n"+
            "<meta charset=\"utf-8\" />"+"\n"+
    "<title>Presentacio Circus</title>"+"\n"+


  "<style type=\"text/css\">"+"\n"+
  "body{"+"\n"+
  "background-color: black;"+"\n"+
 "}"+"\n"+
  "header {"+"\n"+
"font-weight: normal;"+"\n"+
  "text-align: center;"+"\n"+
 "font-size: 50px;"+"\n"+
  "color: #999999"+"\n"+
  "}"+"\n"+
 "</style>"+"\n"+



        "<header>Circus visualization</header>"+"\n"+


            "<script type=\"text/javascript\">"+"\n"+
                    "var numImatge = 1;"+"\n"+
                    "var imatge;"+"\n"+
                    "var numerodefotos ="+ nombreImatges +" ;"+"\n"+
                    "var maxX = 500;"+"\n"+
                    "var maxY = 500;"+"\n"+
                    "var rX;"+"\n"+
                    "var rY;"+"\n"+
                    "var fX;"+"\n"+
                    "var fY;"+"\n"+


            "function retornarLienzo(x) {"+"\n"+
                "var canvas = document.getElementById(x);"+"\n"+
                "if (canvas.getContext) {"+"\n"+
                    "var lienzo = canvas.getContext(\"2d\");"+"\n"+
                    "return lienzo;"+"\n"+
               "} else return false;"+"\n"+
            "}"+"\n"+


             "function incrementar() {"+"\n"+
             "numImatge++;"+"\n"+
             "}"+"\n"+


             "function dibujar() {"+"\n"+
             "lienzo = retornarLienzo(\"lienzo1\");"+"\n"+

             "imatge = new Image();"+"\n"+
             "imatge.src = \"Photos2/Imagen\"+numImatge+\".jpeg\";"+"\n"+
             "rX=imatge.width;"+"\n"+
             "rY=imatge.height;"+"\n"+

             "if(rX>maxX && rX>rY){"+"\n"+
             "fX=maxX;"+"\n"+
             "fY=(maxX/rX)*rY;"+"\n"+
             "}	else if(rY>maxY && rY>rX){"+"\n"+
             "fY=maxY;"+"\n"+
             "fX=(maxY/rY)*rX;"+"\n"+
             "}"+"\n"+
             "else {"+"\n"+
             "fX=rX;"+"\n"+
             "fY=rY;"+"\n"+
             "}"+"\n"+

             "if (lienzo) {"+"\n"+
             "lienzo.clearRect(0,0,800,800);"+"\n"+
             "lienzo.save();"+"\n"+
             "lienzo.drawImage(imatge,100,100,fX,fY);"+"\n"+
             "lienzo.restore(); "+"\n"+
             "}"+"\n"+
             "}"+"\n"+


             "function presentacio() {"+"\n"+
             "for (var i=0; i<numerodefotos;i++){   "+"\n"+
             "setTimeout(setInterval(dibujar,50),4000);"+"\n"+
             "setTimeout(incrementar,i*4000 + 3999);"+"\n"+
             "}"+"\n"+
             "}"+"\n"+



             ""+"\n"+


            "</script>"+"\n"+
            "</head>"+"\n"+
            "<body onLoad=\"presentacio()\">"+"\n"+
               "<div id=\"principal\" align=\"center\">"+"\n"+
                    "<canvas id=\"lienzo1\" width=\"800\" height=\"800\">"+"\n"+
                    "Su navegador no permite utilizar canvas."+"\n"+
                    "</canvas>"+"\n"+
                "</div>"+"\n"+
            "</body>"+"\n"+
            "</html>";




// Creem el fitxer HTML5 a la carpeta on hi han les fotos i el style

PrintStream p=null;
 FileOutputStream fileContent=null;
File file = new File (PathHTML5,NameHTML5+".html");
        try {
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(Presentacio.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            fileContent = new FileOutputStream(file);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Presentacio.class.getName()).log(Level.SEVERE, null, ex);
        }

p= new PrintStream (fileContent);
        p.print(html5);
        p.close();





/*
FileWriter fw = null;
try {
fw = new FileWriter(nom);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter salArch = new PrintWriter(bw);
salArch.print(html5);
salArch.close();

}
catch (IOException ex) {
} */

        // Executem la presentacio (l'arxiu HTML)

        try {

	     URI uri = new URI(PathHTML5+"/"+NameHTML5+".html");
              Desktop desktop = null;
	      if (Desktop.isDesktopSupported()) {
	        desktop = Desktop.getDesktop();
	      }

	      if (desktop != null)
	        desktop.browse(uri);
	    } catch (IOException ioe) {
	      ioe.printStackTrace();
	    } catch (URISyntaxException use) {
	      use.printStackTrace();
	    }


    }



  public static int nombreImatgesDirectori(String directori){

File dir = new File (directori);
String[] fitxers = dir.list();
int numArchius = 0;

if (fitxers == null){
  System.out.println("No hi ha fitxers al directori especificat");
}else {

    int i=0;

  for(i =0; i<fitxers.length; i++) {
    numArchius++;
}}


     return numArchius;
 }



}

